<template>
    <div class="con">
        <el-divider content-position="left"><i class="el-icon-user"></i></el-divider>
        <br>
            <el-form ref="form" :rules="rules" :model="form" label-width="80px">
                <div>
                <el-form-item label="昵称" prop="name">
                    <el-input  @change="checkName" v-model="form.name"></el-input>
                </el-form-item>
                </div>
                <div>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="form.password"></el-input>
                </el-form-item>
                </div>
                <el-button type="success" @click="submitForm('form')">登录</el-button>
            </el-form>
        <br>
        <el-divider content-position="right">欢迎使用HIS管理系统</el-divider>
    </div>
</template>

<script>
    import {HOST} from '../../config/HOST'
    import storage from 'good-storage'
    export default {
        data() {
            return{
                user:'',
                form:{
                    name:'',
                    password:'',
                    topath:''
                },
                rules:{
                    name:[
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    password:[
                        {required: true, message: '请填写密码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods:{
            checkName(){
                this.$ajax.get(`${HOST}isLogin?name=${this.form.name}`,this.form).then((res)=>{
                    this.user=res.data
                    if(res.data.userName==null){
                        this.$message({
                            type:'warning',
                            message:'此帐号不存在！'
                        })
                    }
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(this.user.password==this.form.password){
                            window.localStorage.setItem("username",this.form.name)
                            storage.set("user",this.user)
                            switch (this.user.useType) {
                                case 0:
                                    this.topath='/schedul';
                                    break;
                                case 1:
                                    this.topath='/dispensing';
                                    break;
                                case 2:
                                    this.topath='/register';
                                    break;
                                case 3:
                                    this.topath='/checkApply';
                                    break;
                                case 4:
                                    this.topath='/register';
                                    break;

                            }
                            this.$router.push(this.topath)
                        }else{
                            this.$message({
                                type:'warning',
                                message:'密码错误！'
                            })
                        }
                    } else {
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scoped>
    .con{
        padding-top: 100px;
        text-align: center;
    }
    .el-form-item{
        display: inline-block;
    }
    .el-button{
        position: relative;
        left: 40px;
        width: 150px;
    }
</style>